import main from "./index";
import { Gete } from "./utils";
import { IData } from "./models";

export default class {
  Code: HTMLTextAreaElement;
  
  constructor() {
    this.Code = Gete("sourceCode") as HTMLTextAreaElement;
  }
  
  Update() {
    const data = { palette: [], sprite: [] };
    for (const name in main.Canvas.Data) {
      const { palette } = main.Canvas.Data[name] as IData;
      data.palette.push({ name, data: palette.toString() });
    }
    data.sprite = main.Marker.Data.map(markerItem => {
      const { name, rgbIndexData, bmpData } = markerItem;
      const paletteIndex = rgbIndexData.toString();
      const layer = bmpData.map((bmp: string[]) =>
        bmp.map(row => parseInt(row, 2).toString(16)).toString()
      );
      return { name, paletteIndex, layer };
    });
    this.Code.value = JSON.stringify(data, null, 2);
  }
};
